package com.net.xpay.stat.scheduler;


import com.net.xpay.stat.task.PartnerSafePaySettleDailyTask;
import com.net.xpay.common.helper.NoticeHelper;
import com.net.xpay.common.service.PartnerSafePaySettleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 2019/7/15 16:28

 * 结算合伙人佣金
 */
@Slf4j
@Component
public class PartnerSafePaySettleScheduler {

    @Autowired
    private PartnerSafePaySettleService partnerSafePaySettleService;

    @Autowired
    private PartnerSafePaySettleDailyTask partnerSafePaySettleDailyTask;



    @Autowired
    private NoticeHelper noticeHelper;


    /**
     * 合伙人安全付佣金统计
     */
    @Scheduled(cron = "0 30 0 * * *")
    public void doPartnerSafePaySettleDailyTask() {
        try {
            long t1 = System.currentTimeMillis();
            log.info("start schedule doPartnerSafePaySettleDailyTask|now={}", LocalDateTime.now());
            partnerSafePaySettleDailyTask.startDailyWork();
            long t2 = System.currentTimeMillis();
            log.info("end schedule doPartnerSafePaySettleDailyTask|now={}|cost={}", LocalDateTime.now(), t2 - t1);
            noticeHelper.sendSystemInfoNotice("安全付合伙人佣金统计完成，耗费时间：" + (t2 - t1), "text");
        } catch (Throwable e) {
            log.warn("doPartnerSafePaySettleDailyTask error", e);
            noticeHelper.sendSystemMonitorNotice("安全付合伙人佣金统计失败|doPartnerSafePaySettleDailyTask error", "text");
        }
    }



    /**
     * 月结 每月1号 结算安全付合伙人佣金
     */
    @Scheduled(cron = "0 0 4 1 * ?")
    public void settleMonthSafePayPartnerBenefit() {
        try {
            long t1 = System.currentTimeMillis();
            log.info("start schedule settleMonthSafePayPartnerBenefit|now={}", LocalDateTime.now());
            partnerSafePaySettleService.settleMonthPartnerBenefit();
            long t2 = System.currentTimeMillis();
            log.info("end schedule settleMonthSafePayPartnerBenefit|now={}|cost={}", LocalDateTime.now(), t2 - t1);
            noticeHelper.sendSystemInfoNotice("安全付合伙人月佣金结算记录生成完成，耗费时间：" + (t2 - t1), "text");
        } catch (Throwable e) {
            log.warn("settleMonthSafePayPartnerBenefit error", e);
            noticeHelper.sendSystemMonitorNotice("安全付合伙人月佣金结算失败|settleMonthSafePayPartnerBenefit error", "text");
        }
    }

    /**
     * 日结 结算安全付合伙人佣金
     */
    @Scheduled(cron = "0 30 4 * * ?")
    public void settleDaySafePayPartnerBenefit() {
        try {
            long t1 = System.currentTimeMillis();
            log.info("start schedule settleDaySafePayPartnerBenefit|now={}", LocalDateTime.now());
            partnerSafePaySettleService.settleDayPartnerBenefit();
            long t2 = System.currentTimeMillis();
            log.info("end schedule settleDaySafePayPartnerBenefit|now={}|cost={}", LocalDateTime.now(), t2 - t1);
            noticeHelper.sendSystemInfoNotice("安全付合伙人日佣金结算记录生成完成，耗费时间：" + (t2 - t1), "text");
        } catch (Throwable e) {
            log.warn("settleDaySafePayPartnerBenefit error", e);
            noticeHelper.sendSystemMonitorNotice("安全付合伙人日佣金结算失败|settleDaySafePayPartnerBenefit error", "text");
        }
    }
}
